
package org.okkam.refine.strategy;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.xml.bind.JAXBException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.okkam.client.data.AttributeMetadataType;
import org.okkam.client.data.AttributeType;
import org.okkam.client.data.AttributesType;
import org.okkam.client.data.Entity;
import org.okkam.client.data.ProfileType;
import org.okkam.client.util.XMLEntityConverter;
import org.okkam.refine.commands.CreateEntityToENSCommand;
import org.okkam.refine.strategy.CandidateStrategy;
import org.okkam.refine.strategy.GenericCandidate;

public class GenericCandidate implements CandidateStrategy {

    private static Log log = LogFactory.getLog(GenericCandidate.class);

    @Override
    public boolean checkEquality(List<AttributeType> attributeTypeList, List<String> entityList) {
       
        Iterator<AttributeType> attributeTypeIterator = attributeTypeList.iterator();
        Map<String, String> propValueNewEntity = new HashMap<String, String>();
        boolean match = false;
        
        while (attributeTypeIterator.hasNext()) {
            AttributeType attributeType = attributeTypeIterator.next();
            String term = attributeType.getName().getLocalPart().toLowerCase();
            String value = attributeType.getValue().toLowerCase().replace("\"", "");
            AttributeMetadataType metadata = attributeType.getMetadata();
            propValueNewEntity.put(term, value);
        }            
        
        Iterator entityIterator = entityList.iterator();
        while (entityIterator.hasNext()) {
                XMLEntityConverter converter = new XMLEntityConverter();
                Entity entity = null;
                try {
                    entity = converter.xmlToEntity( entityIterator.next().toString());
                } catch (JAXBException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                ProfileType profile = entity.getProfile();
                AttributesType attributesType = profile.getAttributes();
                List<AttributeType> attributeTypeListResponse = attributesType.getAttributes();
                Iterator<AttributeType> attributeTypeIteratorresponse = attributeTypeListResponse.iterator();

                Map<String, String> propValueResponse = new HashMap<String, String>();

                while (attributeTypeIteratorresponse.hasNext()) {
                        AttributeType attributeType = attributeTypeIteratorresponse.next();
                        String term = attributeType.getName().getLocalPart().toLowerCase();
                        String value = attributeType.getValue().toLowerCase().replace("\"", "");
                        AttributeMetadataType metadata = attributeType.getMetadata();
                        propValueResponse.put(term, value);
                }
                    
                Iterator it = propValueNewEntity.keySet().iterator();

          //generic attribute Matching
            int firstLength = 0;
            int secondLength = 0;
            int thirdLength = 0;
            if(CreateEntityToENSCommand.getFirst()!=null)
                firstLength = CreateEntityToENSCommand.getFirst().length;
            if(CreateEntityToENSCommand.getSecond()!=null)
                secondLength = CreateEntityToENSCommand.getSecond().length;
            if(CreateEntityToENSCommand.getThird()!=null)
                thirdLength = CreateEntityToENSCommand.getThird().length;
           
           //check a match with a primary matching attributes
            if(firstLength>0){ 
                for (int i=0;i<firstLength;i++){
                    String attribute = CreateEntityToENSCommand.getFirst()[i];
                    if (propValueResponse.containsKey(attribute)) {
                            String valOfQuery = propValueNewEntity.containsKey(attribute)?propValueNewEntity.get(attribute).replace("\"", ""):"NULL";
                            if (valOfQuery.equals(propValueResponse.get(attribute).toLowerCase())&&valOfQuery!="NULL") {
                                match = true;
                        }
                            else {
                                match = false;
                                break;
                            }
                    } 
                    else {
                        match = false;
                        break;
                    }
                }
            }
            if (match){
                return match;
            }
          //check a match with a Secondary matching attributes
            if(secondLength>0){ 
                for (int i=0;i<secondLength;i++){
                    String attribute = CreateEntityToENSCommand.getSecond()[i];
                    if (propValueResponse.containsKey(attribute)) {
                        String valOfQuery = propValueNewEntity.containsKey(attribute)?propValueNewEntity.get(attribute).replace("\"", ""):"NULL";
                        if (valOfQuery.equals(propValueResponse.get(attribute).toLowerCase())&&valOfQuery!="NULL") {
                                match = true;
                        }
                            else {
                                match = false;
                                break;
                            }
                    } 
                    else {
                        match = false;
                        break;
                    }
                }
            }
            if (match){
                return match;
            }
          //check a match with a third matching attributes
            if(thirdLength>0){ 
                for (int i=0;i<thirdLength;i++){
                    String attribute = CreateEntityToENSCommand.getThird()[i];
                    if (propValueResponse.containsKey(attribute)) {
                        String valOfQuery = propValueNewEntity.containsKey(attribute)?propValueNewEntity.get(attribute).replace("\"", ""):"NULL";
                        if (valOfQuery.equals(propValueResponse.get(attribute).toLowerCase())&&valOfQuery!="NULL") {
                                match = true;
                        }
                            else {
                                match = false;
                                break;
                            }
                    } 
                    else {
                        match = false;
                        break;
                    }
                }
            }
            if (match){
                return match;
            }
        }
        return match;//only if not a match
    }
}
